package com.wise.site.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.wise.site.R;
import com.wise.site.protocol.action.InfoListAction;
import com.wise.site.protocol.action.ShopListAction;
import com.wise.site.protocol.base.ProtocolManager;
import com.wise.site.protocol.base.SoapAction.ACTION_TYPE;
import com.wise.site.protocol.base.SoapAction.ActionListener;
import com.wise.site.protocol.result.InfoItem;
import com.wise.site.protocol.result.InfoItemsResult;
import com.wise.site.protocol.result.ShopItem;
import com.wise.site.protocol.result.ShopItemsResult;
import com.wise.site.utils.FileCache;
import com.wise.site.utils.ImageLoader;
import com.wise.site.utils.ImageLoader.Imager;
import com.wise.site.utils.Log;
import com.wise.site.widget.PullRefenceList;
import com.wise.site.widget.PullRefenceList.OnRefreshListener;

public class InfoListAdatper extends ArrayListAdapter {
	public enum ITEM_TYPE{
		//资讯类
		INFO_TYPE,
		//资讯类头条
		INFO_HEADLINE_TYPE,
		//店铺类头条
		SHOP_HEADLINE_TYPE,
		//店铺类普通
		SHOP_TYPE;
	}	
	
	private ITEM_TYPE type;
	private InfoListAction mInfoAction;
	private ShopListAction mShopAction;
	private OnClickListener mOnClickListener;
	
	private Handler mHandler = new Handler();

	public InfoListAdatper(ITEM_TYPE type, Context context) {
		super(context);
		this.type = type;
		if(type == ITEM_TYPE.INFO_TYPE|| type == ITEM_TYPE.INFO_HEADLINE_TYPE){
			mInfoAction = new InfoListAction(ACTION_TYPE.ACTION_COMMON, "getmessinfo");
			mInfoAction.moveToFristPage();
		} else if(type == ITEM_TYPE.SHOP_TYPE || type == ITEM_TYPE.SHOP_HEADLINE_TYPE){
			mShopAction = new ShopListAction(ACTION_TYPE.ACTION_COMMON, "shopinfo");
		}
	}
	
	public InfoListAdatper(ITEM_TYPE type, Context context, String method) {
		super(context);
		this.type = type;
		if(type == ITEM_TYPE.INFO_TYPE|| type == ITEM_TYPE.INFO_HEADLINE_TYPE){
			mInfoAction = new InfoListAction(ACTION_TYPE.ACTION_COMMON, method);
			mInfoAction.moveToFristPage();
		} else if(type == ITEM_TYPE.SHOP_TYPE || type == ITEM_TYPE.SHOP_HEADLINE_TYPE){
			mShopAction = new ShopListAction(ACTION_TYPE.ACTION_COMMON, method);
		}
	}
	
	public void addJsonParam(String param, Object value){
		if(type == ITEM_TYPE.INFO_TYPE|| type == ITEM_TYPE.INFO_HEADLINE_TYPE){
			mInfoAction.addJsonParam(param, value);
		} else if(type == ITEM_TYPE.SHOP_TYPE || type == ITEM_TYPE.SHOP_HEADLINE_TYPE){
			mShopAction.addJsonParam(param, value);
		}
	}
	
	public void moveToNextPage(){
		if(type == ITEM_TYPE.INFO_TYPE|| type == ITEM_TYPE.INFO_HEADLINE_TYPE){
			mInfoAction.moveToNextPage();
		} else if(type == ITEM_TYPE.SHOP_TYPE || type == ITEM_TYPE.SHOP_HEADLINE_TYPE){
			mShopAction.moveToNextPage();
		}
	}
	
	public void moveToFristPage(){
		if(type == ITEM_TYPE.INFO_TYPE|| type == ITEM_TYPE.INFO_HEADLINE_TYPE){
			mInfoAction.moveToFristPage();
		} else if(type == ITEM_TYPE.SHOP_TYPE || type == ITEM_TYPE.SHOP_HEADLINE_TYPE){
			mShopAction.moveToFristPage();
		}
	}
	
	/**
	 * 设置点击标签的id
	 *
	 * huangyue  2012-9-24
	 */
	public void setTypeId(int id){
		if(type == ITEM_TYPE.INFO_TYPE|| type == ITEM_TYPE.INFO_HEADLINE_TYPE){
			mInfoAction.setTypeId(id);
		} else if(type == ITEM_TYPE.SHOP_TYPE || type == ITEM_TYPE.SHOP_HEADLINE_TYPE){
			mShopAction.setTypeId(id);
		}
	}
	
	/**
	 *
	 * huangyue  2012-9-27
	 */
	public void setType(int type){
		if(this.type == ITEM_TYPE.INFO_TYPE|| this.type == ITEM_TYPE.INFO_HEADLINE_TYPE){
			mInfoAction.addJsonParam("type", type);
		} else if(this.type == ITEM_TYPE.SHOP_TYPE || this.type == ITEM_TYPE.SHOP_HEADLINE_TYPE){
			mShopAction.addJsonParam("type", type);
		}
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(type == ITEM_TYPE.INFO_TYPE|| type == ITEM_TYPE.INFO_HEADLINE_TYPE){
			convertView = infoView(position, convertView);
		}else if(type == ITEM_TYPE.SHOP_TYPE || type == ITEM_TYPE.SHOP_HEADLINE_TYPE){
			convertView = shopView(position, convertView);
		}
		
		if(mOnClickListener != null){
			convertView.setOnClickListener(mOnClickListener);
		}
		
		return convertView;
	}
	
	public void setKeyword(String keyword){
		mShopAction.setKeyword(keyword);
	}
	
	public OnClickListener getOnClickListener() {
		return mOnClickListener;
	}

	public void setOnClickListener(OnClickListener onClickListener) {
		this.mOnClickListener = onClickListener;
	}

	private View shopView(int position, View convertView) {
		convertView = inflater.inflate(R.layout.list_shop_item, null);
		
		ShopItem item = (ShopItem) mList.get(position);
		convertView.setTag(item);
		ImageView image = (ImageView) convertView.findViewById(R.id.shop_icon);
		ImageView vip = (ImageView) convertView.findViewById(R.id.vip_icon);
		if(item.logoImageUrl != null && item.logoImageUrl.length() > 5){
			ImageLoader.getInstance().DisplayImage( new Imager(item.logoImageUrl, image));
		} else {
			image.setImageResource(R.drawable.shop_default);
			image.invalidate();
		}
		
		if(item.level == 2){
			vip.setBackgroundResource(R.drawable.l_1);
		} else if(item.level == 3){
			vip.setBackgroundResource(R.drawable.l_2);
		}else if(item.level == 4) {
			vip.setBackgroundResource(R.drawable.l_3);
		}
		((TextView)convertView.findViewById(R.id.title)).setText(item.shopName);
		((TextView)convertView.findViewById(R.id.phone)).setText(item.phone);
		((TextView)convertView.findViewById(R.id.addr)).setText(item.address);
		
		if(type == ITEM_TYPE.SHOP_HEADLINE_TYPE) {
			convertView.findViewById(R.id.right_arrow).setVisibility(View.INVISIBLE);
		}
		return convertView;
	}

	private View infoView(int position, View convertView) {
		convertView = inflater.inflate(R.layout.list_info_item, null);
		
		InfoItem item = (InfoItem) mList.get(position);
		convertView.setTag(item);
		ImageView image = (ImageView) convertView.findViewById(R.id.icon_image);
		if(item.iconURL != null && item.iconURL.length() > 5){
			ImageLoader.getInstance().DisplayImage( new Imager(item.iconURL, image, mContext.getResources().getDimensionPixelSize(R.dimen.mult_ima_height), 0));
		} else {
			image.setImageResource(R.drawable.info_default);
		}
		
		if(item.headed){
			convertView.findViewById(R.id.headline_icon).setVisibility(View.VISIBLE);
		} else {
			convertView.findViewById(R.id.headline_icon).setVisibility(View.GONE);
		}
		((TextView)convertView.findViewById(R.id.title)).setText(item.tilte);
		((TextView)convertView.findViewById(R.id.info)).setText(item.snipe);
		((TextView)convertView.findViewById(R.id.date)).setText(item.date);
		return convertView;
	}
	
	/**
	 * 设置listView
	 */
	public void setListView(ListView listView) {
		super.setListView(listView);
		mListView.setAdapter(this);
		if(mListView instanceof PullRefenceList){
			PullRefenceList list = (PullRefenceList)mListView;
			list.setonRefreshListener(new OnRefreshListener() {
				@Override
				public void onRefresh() {
					removeAll();
					moveToFristPage();
					((PullRefenceList)mListView).hiddenFoot();
					loadData();
				}
			});
			
			list.setFootOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					moveToNextPage();
					loadData(true);
				}
			});
		}
	}
	
	
	public void loadData(){
		loadData(false);
	}
	
	public void loadData(final boolean add) {
		showProgress();
		 
		if(type == ITEM_TYPE.INFO_TYPE || type == ITEM_TYPE.INFO_HEADLINE_TYPE){
			mInfoAction.setActionListener(new ActionListener<InfoItemsResult>() {
				public void onSucceed(InfoItemsResult result) {
					if(result == null || result.list == null){
						return;
					}
					
					if(add){
						addItems(result.list);
					} else {
						setList(result.list);
						FileCache.getInstance().put("info_" + mInfoAction.getJsonParam("mtype"), result, true);
					}
					
					if(ITEM_TYPE.INFO_HEADLINE_TYPE == type){
						FileCache.getInstance().put("headline_info", result, true);
						LayoutParams lp = mListView.getLayoutParams();
						lp.height = mContext.getResources().getDimensionPixelSize(R.dimen.item_height) * result.list.size();
						mListView.requestLayout();
					} 
					
					refenceComplete(result.list);
				}
				public void onError(int resultCode) {
					Log.w("InfoListAction", "resultCode" + resultCode);
					onSucceed((InfoItemsResult)FileCache.getInstance().get("info_" + mInfoAction.getJsonParam("mtype")));
					refenceComplete(null);
				}
			});
	        ProtocolManager.getProtocolManager().submitAction(mInfoAction);
		} else if(type == ITEM_TYPE.SHOP_TYPE || type == ITEM_TYPE.SHOP_HEADLINE_TYPE){
			if(type == ITEM_TYPE.SHOP_HEADLINE_TYPE){
				mShopAction.addJsonParam("top", 1);
			}
			
			mShopAction.setActionListener(new ActionListener<ShopItemsResult>() {

				@Override
				public void onSucceed(ShopItemsResult result) {
					if(result == null){
						return;
					}
					if(add){
						addItems(result.list);
					} else {
						setList(result.list);
						if(type == ITEM_TYPE.SHOP_HEADLINE_TYPE){
							FileCache.getInstance().put("shop_HEADLINE", result, true);
						} else {
							FileCache.getInstance().put("shop_" + mShopAction.getJsonParam("type"), result, true);
						}
					}
					refenceComplete(result.list);
				}

				@Override
				public void onError(int resultCode) {
					Log.w("ShopItemsResult", "resultCode" + resultCode);
					if(type == ITEM_TYPE.SHOP_HEADLINE_TYPE){
						onSucceed((ShopItemsResult)FileCache.getInstance().get("shop_HEADLINE"));
					} else {
						try{
							onSucceed((ShopItemsResult)FileCache.getInstance().get("shop_" + mShopAction.getJsonParam("type")));
						}catch (Exception e) {
							Log.w("InfoListAdatper", "", e);
						}
					}
					refenceComplete(null);
				}
			});
	        ProtocolManager.getProtocolManager().submitAction(mShopAction);
		}
	}

}
